package com.chisheng.caa.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chisheng.caa.pojo.*;
import com.chisheng.caa.pojo.dto.*;
import com.chisheng.caa.pojo.response.JsonResponse;
import com.chisheng.caa.pojo.response.PageDTO;
import com.chisheng.caa.service.*;
import com.chisheng.caa.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@CrossOrigin
@Api("数据服务控制器")
@RestController
@RequestMapping(value = "/api/caa/data", produces = MediaType.APPLICATION_JSON_VALUE)
public class DataController {

    @Resource
    private CollegeService collegeService;

    @Resource
    private CourseAffiliationService courseAffiliationService;

    @Resource
    private CourseNatureService courseNatureService;

    @Resource
    private CourseTypeService courseTypeService;

    @Resource
    private MajorService majorService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private CourseService courseService;

    @ApiOperation("请求学院信息列表")
    @GetMapping("/college/list")
    public JsonResponse<List<CollegeDTO>> collegeList() {
        return JsonResponse.success(collegeService.getCollegeList());
    }

    @ApiOperation("请求课程归属信息列表")
    @GetMapping("/course-affiliation/list")
    public JsonResponse<List<CourseAffiliationDTO>> courseAffiliationList() {
        return JsonResponse.success(courseAffiliationService.getCourseAffiliationList());
    }

    @ApiOperation("请求课程性质信息列表")
    @GetMapping("/course-nature/list")
    public JsonResponse<List<CourseNatureDTO>> courseNatureList() {
        return JsonResponse.success(courseNatureService.getCourseNatureList());
    }

    @ApiOperation("请求课程类型信息列表")
    @GetMapping("/course-type/list")
    public JsonResponse<List<CourseTypeDTO>> courseTypeList() {
        return JsonResponse.success(courseTypeService.getCourseTypeList());
    }

    @ApiOperation("请求专业信息列表")
    @GetMapping("/major/list")
    public JsonResponse<List<MajorDTO>> majorList() {
        return JsonResponse.success(majorService.getMajorList());
    }

    @ApiOperation("请求教师信息列表")
    @GetMapping("/teacher/list")
    public JsonResponse<List<TeacherDTO>> teacherList() {
        return JsonResponse.success(teacherService.getTeacherList());
    }

    @ApiOperation("请求年级年份列表")
    @GetMapping("/grade-year/list")
    public JsonResponse<Collection<Map<String, Object>>> gradeYearList() {
        List<Integer> yearList = courseService.getGradeYearList();
        return JsonResponse.success(ResponseUtil.kvList(yearList));
    }

    @ApiOperation("请求课程页")
    @PostMapping("/course/page/{current}/{size}")
    public JsonResponse<PageDTO<CourseDTO>> coursePage(@ApiParam("当前页") @PathVariable Long current,
                                                       @ApiParam("每页显示条数") @PathVariable Long size,
                                                       @ApiParam("查询条件") @Nullable @RequestBody(required = false) CourseQuery query) {
        return JsonResponse.success(courseService.getPage(new Page<>(current, size), query));
    }

    @ApiOperation("请求冲突处理策略列表")
    @GetMapping("/strategy/duplicate/list")
    public JsonResponse<List<Map<String, Object>>> duplicateStrategyList() {
        List<Map<String, Object>> valueDescList = ResponseUtil.getValueDescList(DuplicateStrategy.values());
        return JsonResponse.success(valueDescList);
    }

    @ApiOperation("请求加载策略列表")
    @GetMapping("/strategy/loading/list")
    public JsonResponse<List<Map<String, Object>>> loadingStrategyList() {
        List<Map<String, Object>> valueDescList = ResponseUtil.getValueDescList(LoadingStrategy.values());
        return JsonResponse.success(valueDescList);
    }

    @ApiOperation("请求学期列表")
    @GetMapping("/semester/list")
    public JsonResponse<List<Map<String, Object>>> termList() {
        List<Map<String, Object>> valueDescList = ResponseUtil.getValueDescList(Semester.values());
        return JsonResponse.success(valueDescList);
    }

}
